﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PuSuoPai;
using ServiceStack.Redis;
using ServiceStack.Redis.Generic;
using System.Threading;

namespace TeZhanDui
{
    public class RoomControl
    {
        public const int ROOM_COUNT = 100;
        public Dictionary<int, Room> roomDict;
        static public readonly RoomControl Instance = new RoomControl();
        Timer monitorRoomListTimer;
        public List<int> roomInfos;
        
        RoomControl()
        {
            roomDict = new Dictionary<int, Room>();
            roomInfos = new List<int>();
            StartMonitorRoomList();
        }

        void StartMonitorRoomList()
        {
            monitorRoomListTimer =  new Timer(UpdateRoomList, null, 1000, 1000);
        }

        void UpdateRoomList(object o)
        {
            roomInfos.Clear();
            for (int i = 1; i <= RoomControl.ROOM_COUNT; i++)
            {
                int playerCount = 0;
                if (IsRoomExist(i))
                {
                    playerCount = RoomControl.Instance.roomDict[i].playerCount;
                }
                roomInfos.Add(playerCount);
            }
        }

        public Boolean IsRoomExist(int roomId)
        {
            return roomDict.ContainsKey(roomId);
        }

        public void AddRoom(RoomMessageReq req)
        {
            Room room = new Room();
            room.roomId = req.roomId;
            room.type = req.type;
            room.state = ROOM_STATE.WAITING;
            
            roomDict.Add(room.roomId, room);

            // for test sake, add 5 fake player in room 7
//            if (req.roomId == 7)
//            {
//                for (int i = 0; i < 5; i++)
//                {
//                    using (var redis = RedisMgr.Instance.GetClient())
//                    {
//                        IRedisTypedClient<Player> redisT = redis.As<Player>();
//                        Player player = redisT.GetFromHash((13818767376 + i).ToString());
//                        room.positions[i].player = player;
//                        redisT.StoreAsHash(player);
//                        Console.WriteLine("player " + player.Id + "point="+ player.Points+",fake sit down");
//                    }
//                }
//            }

            // for test sake, add 3 fake player in room 7
            /*if (req.roomId == 7)
            {
                for (int i = 0; i < 3; i++)
                {
                    using (var redis = RedisMgr.Instance.GetClient())
                    {
                        IRedisTypedClient<Player> redisT = redis.As<Player>();
                        Player player = redisT.GetFromHash((13818767376 + i).ToString());
                        room.positions[i/2*3 + i%2].player = player;
                        redisT.StoreAsHash(player);
                        Console.WriteLine("player " + player.Id + "point=" + player.Points + ",fake sit down");
                    }
                }
            }*/
        }

        public RoomMessageResp RoomControlFsm(RoomMessageReq req)
        {
            Room room = roomDict[req.roomId];
            Boolean success = room.RoomProcessMessage(req);
            RoomMessageResp resp = new RoomMessageResp();
            resp.success = success;
            resp.state = room.state;
            resp.countDownTimerCount = room.countDownTimerCount;
            /*resp.pos1 = room.positions[0];
            resp.pos2 = room.positions[1];
            resp.pos3 = room.positions[2];
            resp.pos4 = room.positions[3];
            resp.pos5 = room.positions[4];
            resp.pos6 = room.positions[5];*/
            for (int i = 0; i < 6; i++)
            {
                //resp.positions[i] = room.positions[i];
                resp.positions.Add(room.positions[i]);
            }
            return resp;
        }

        public GameMessageResp GameControlFsm(GameMessageReq req)
        {
            Room room = roomDict[req.roomId];
            if (room.game == null || room.game.state == GAME_STATE.CLOSE)
            {
                GameMessageResp resp = new GameMessageResp();
                resp.success = false;
                resp.state = GAME_STATE.CLOSE;
                return resp;
            }
            return room.game.GameProcessMessage(req);
        }
    }
}
